// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using NUnit.Framework;
using ValidationAspects.Exceptions;
using ValidationAspects.Factories;

namespace TestValidationAspects.Factories
{
	[TestFixture]
	public class TestIsDate
	{
		private static readonly IsDate IsDate = new IsDate();

		private static void Fail(string value)
		{
			Test.Throws<ValidateIsDateException>(() => IsDate.Create<string>().Invoke(value, new MockValidationContext()), new ValidateIsDateException(IsDate.Format, value).Message);
		}

		private static void Pass(string value)
		{
			IsDate.Create<string>().Invoke(value, new MockValidationContext());
		}

		[Test]
		public void Test_default_Format()
		{
			Assert.IsNull(new IsDate().Format);
		}

		[Test]
		public void Test_validation_skips_null_value()
		{
			IsDate.Format = "DDMMYY";
			Pass(null);
		}

		[Test]
		public void Test_validation_skips_null_Format()
		{
			IsDate.Format = null;
			Pass("150609");
		}

		[Test]
		public void Test_validation_skips_empty_Format()
		{
			IsDate.Format = string.Empty;
			Pass("150609");
		}

		[Test]
		public void Test_validation()
		{
			IsDate.Format = "dd/MM/yyyy";
			Pass("15/06/2009");
			Fail("15062009");
			Fail("32/06/2009");
			Fail("a");

			IsDate.Format = "ddMMyyyy";
			Pass("15062009");
			Fail("15/06/2009");
			Fail("32062009");
			Fail("a");

			IsDate.Format = "dd MMMM";
			Pass("15 June");
			Fail("15 06");
			Fail("32 June");
			Fail("a");
		}
	}
}